/*
 * $Id: JAboutDialog.java 40 2009-12-24 14:58:58Z aaron.day $
 */
package org.crazydays.gameplan.swing;


import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.swing.JDialog;
import javax.swing.JEditorPane;

import org.crazydays.i18n.Messages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * AboutDialog
 */
public class JAboutDialog
    extends JDialog
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: JAboutDialog.java 40 2009-12-24 14:58:58Z aaron.day $".hashCode();

    /** logger */
    protected static Logger LOGGER =
        LoggerFactory.getLogger(JAboutDialog.class);

    /** messages */
    public final static Messages MESSAGES = Messages.getInstance();

    /** resource key: title */
    public final static String RESKEY_TITLE = "JAboutDialog.title";

    /** editor pane */
    protected JEditorPane editorPane;

    /**
     * JAboutDialog constructor.
     * 
     * @param frame Parent frame
     */
    public JAboutDialog(Frame frame)
    {
        super(frame, MESSAGES.format(RESKEY_TITLE), true);

        initialize();
    }

    /**
     * Initialize about dialog.
     */
    protected void initialize()
    {
        initializeEditorPane();
        pack();
        initializeLocation();
    }

    /**
     * Initialize location.
     */
    protected void initializeLocation()
    {
        int x =
            getParent().getX() + (getParent().getWidth() / 2)
                - (getWidth() / 2);
        int y =
            getParent().getY() + (getParent().getHeight() / 2)
                - (getHeight() / 2);

        setLocation(new Point(x, y));
    }

    /**
     * Initialize editor pane.
     */
    protected void initializeEditorPane()
    {
        editorPane = new JEditorPane();
        editorPane.setPreferredSize(new Dimension(300, 400));

        // setup content
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
        try {
            // TODO: revist and cleanup
            URL url =
                new URL("file://" + new File(".").getAbsolutePath()
                    + File.separator + "doc" + File.separator + "about.html");

            editorPane.setPage(url);
        } catch (IOException e) {
            LOGGER.warn(new File(".").getAbsolutePath());
            editorPane.setText("Unable to load about.html");
        }

        add(editorPane);
    }
}
